package com.zhuiyun.project.api.record.controller;

import cn.hutool.json.JSONObject;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.collectVideo.model.FavoritesModel;
import com.zhuiyun.project.api.record.entity.Record;
import com.zhuiyun.project.api.record.model.RecordModel;
import com.zhuiyun.project.api.record.service.RecordService;
import com.zhuiyun.project.api.topicmanage.entity.Topic;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName RecordController
 * @Description TODO 观看记录模块
 * @Author XC8
 * @Date 2024/2/26 14:39
 **/
@RestController
@RequestMapping("recordController")
@Api(tags = "观看记录")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
    * @Author XC8
    * @Description 观看记录列表
    * @Date 2024/2/26 14:44
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @GetMapping("/getRecordById")
    @ApiOperation(value = "根据id用户信息")
    public CommonResult getRecordById(String token) throws CommonException {
        String jsonstr = stringRedisTemplate.opsForValue().get(token);
        if (jsonstr == null || jsonstr.equals("")) {
            return CommonResult.ok(null);
        } else {
            // 使用JSONObject解析ISON字符串
            JSONObject json0bject = new JSONObject(jsonstr);
            // 获取id字段的值
            int userId = json0bject.getInt("id");
            return CommonResult.ok(recordService.getRecordById(userId,null));
        }

    }

    /**
    * @Author XC8
    * @Description 根据id删除观看记录
    * @Date 2024/2/26 15:38
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @PostMapping("recordDelete")
    @OptLog(modular = "观看记录",type = CommonConstants.DELETE,describe = "观看记录删除方法")
    public CommonResult recordDelete(Integer rId){
        System.out.println("进入删除方法");
        System.out.println(rId);
        return recordService.recordDelete(rId);
    }

    /**
    * @Author XC8
    * @Description 生成观看记录
    * @Date 2024/3/8 9:27
    * @Return com.zhuiyun.project.common.response.CommonResult

    */
    @PutMapping("addRecord")
    @ApiOperation(value = "新增前端观看记录")
    public CommonResult addRecord(@RequestBody RecordModel recordModel)throws CommonException{
        return CommonResult.ok(recordService.addRecord(recordModel));
    }

}
